﻿using Microsoft.Extensions.Logging;
using System.IO;
using System.Windows;

namespace 充电桩上位机;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    private static readonly string _log = Path.Combine
    (Path.GetDirectoryName(Environment.ProcessPath)!, @"logging.txt");
    private readonly ILoggerFactory _loggerFactory = LoggerFactory.Create(builder =>
    {
        builder.AddConsole(options =>
        {
            options.FormatterName = "ConsoleLoggerFormat.Systemd";
        })
    .AddFile(options =>
    {
        options.BaseDirectory = ".";//对于需要将日志输出到其他位置的非常有用
        options.IncludeScopes = true; //表示是否输出日志范围，默认输出
        options.InternalLogFile = _log;
    });
    });
    public static ILogger<App>? _logger;
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        _logger = _loggerFactory.CreateLogger<App>();
        DispatcherUnhandledException += App_DispatcherUnhandledException;
        //AnimationView animationView = new();
        //animationView.Show();
        //Thread.Sleep(5000);

    }
    private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        e.Handled = true;
        _logger!.LogInformation(e.Exception.Message.ToString());
    }
}
